package com.aids_malaria.util;

import android.text.TextUtils;

import com.aids_malaria.R;
import com.aids_malaria.app.App;
import com.aids_malaria.app.Constant;
import com.aids_malaria.db.DBManager;
import com.aids_malaria.http.response.PadMaxIdentitySeq;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Author: VVP
 * Time: 2018/11/19 15:36.
 * Desc:
 */

public class StringUtil {

    /**
     * 判断字符串是否有值，如果为null或者是空字符串或者只有空格或者为"null"字符串，则返回true，否则则返回false
     */
    public static boolean isEmpty(String value) {
        return !(value != null && !"".equalsIgnoreCase(value.trim())
                && !"null".equalsIgnoreCase(value.trim()));
    }

    /**
     * 判断字符串是否为纯数字
     *
     * @param str 字符串
     * @return 是否纯数字
     */
    public static boolean isNumber(String str) {
        for (int i = 0; i < str.length(); i++) {
            if (!Character.isDigit(str.charAt(i))) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断手机号字符串是否合法
     *
     * @param phoneNumber 手机号字符串
     * @return 手机号字符串是否合法
     */
    public static boolean isPhoneNumberValid(String phoneNumber) {
        boolean isValid = false;
        String expression = "^1[3|4|5|6|7|8|9]\\d{9}$";
        CharSequence inputStr = phoneNumber;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

    /**
     * 判断手机号字符串是否合法
     *
     * @param areaCode    区号
     * @param phoneNumber 手机号字符串
     * @return 手机号字符串是否合法
     */
    public static boolean isPhoneNumberValid(String areaCode, String phoneNumber) {
        if (TextUtils.isEmpty(phoneNumber)) {
            return false;
        }

        if (phoneNumber.length() < 5) {
            return false;
        }

        if (TextUtils.equals(areaCode, "+86") || TextUtils.equals(areaCode, "86")) {
            return isPhoneNumberValid(phoneNumber);
        }

        boolean isValid = false;
        String expression = "^[0-9]*$";
        CharSequence inputStr = phoneNumber;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

    /**
     * 判断字符串是否是手机号格式
     *
     * @param areaCode    区号
     * @param phoneNumber 手机号字符串
     * @return 字符串是否是手机号格式
     */
    public static boolean isPhoneFormat(String areaCode, String phoneNumber) {
        if (TextUtils.isEmpty(phoneNumber)) {
            return false;
        }

        if (phoneNumber.length() < 7) {
            return false;
        }

        boolean isValid = false;
        String expression = "^[0-9]*$";
        CharSequence inputStr = phoneNumber;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

    /**
     * 判断字符串是否是邮箱
     *
     * @param email email
     * @return 字符串是否是邮箱
     */
    public static boolean isEmail(String email) {
        String str = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        Pattern p = Pattern.compile(str);
        Matcher m = p.matcher(email);
        return m.matches();
    }

    /**
     * 生成身份码
     * @param county_code
     * @param birthday
     * @param sex
     * @return
     */
    public static String getIdentity_id(String county_code,String birthday,String sex ){
        String sIdentity_id="";
        try{
            //get the sPad_id
            String sPad_id = SpUtil.getString(App.getAppContext(), Constant.PAD_SEQ, "");
            sPad_id = sPad_id.substring(sPad_id.length()-2,sPad_id.length());
            //get the one bit of county_code,the last bit
            String sCounty_code = county_code.substring(county_code.length()-1, county_code.length());
            //get the birthday bit,remove the - char
            String sBirthday = birthday.replaceAll("-", "");
            //get the max IdentitySeq of exist,this is from database,local get from the PadMaxIdentitySeq array that pad saved
            List<PadMaxIdentitySeq> padMaxIdentitySeqList = DBManager.get().padMaxIdentitySeqDao().getByCodeAndSex(county_code + sBirthday + sPad_id,sex);
            LogUtil.d("padMaxIdentitySeqList:"+GsonUtil.beanToJson(DBManager.get().padMaxIdentitySeqDao().getAll()));
            String sMaxIdentitySeq = "";//保证3位字符串
            if(padMaxIdentitySeqList.size() > 0){
                LogUtil.d("padMaxIdentitySeq:"+GsonUtil.beanToJson(padMaxIdentitySeqList.get(0)));
                sMaxIdentitySeq = padMaxIdentitySeqList.get(0).getMax_identityseq();
            }
            if(sMaxIdentitySeq.equals("")){
                sMaxIdentitySeq="000";
            }
            int nMaxIdentitySeq = Integer.parseInt(sMaxIdentitySeq);
            // according the sex man is add 1 woman is add 2
            if("1".equals(sex)){
                nMaxIdentitySeq+=2;
            } else{
                nMaxIdentitySeq+=2;
            }
            // append 0 if not have three bit
            String sNewIdentitySeq = String.format("%03d", nMaxIdentitySeq);
            // get the check bit
            String sCheck="";
            //the multiplier of each bit see the document
            int nShenshu15 =9  ;
            int nShenshu14 =2  ;
            int nShenshu13 =1  ;
            int nShenshu12 =6  ;
            int nShenshu11 =3  ;
            int nShenshu10 =7  ;
            int nShenshu9  =9  ;
            int nShenshu8  =10 ;
            int nShenshu7  =5  ;
            int nShenshu6  =8  ;
            int nShenshu5  =4  ;
            int nShenshu4  =8  ;
            int nShenshu3  =4  ;
            int nShenshu2  =2  ;
            //the check bit compare value
            String sCheckpairArray[] = new String[11];
            sCheckpairArray[0]="1";
            sCheckpairArray[1]="0";
            sCheckpairArray[2]="X";
            sCheckpairArray[3]="9";
            sCheckpairArray[4]="8";
            sCheckpairArray[5]="7";
            sCheckpairArray[6]="6";
            sCheckpairArray[7]="5";
            sCheckpairArray[8]="4";
            sCheckpairArray[9]="3";
            sCheckpairArray[10]="2";
            int nMod=11;
            int nCheck=(Integer.parseInt(sCounty_code)*nShenshu15+Integer.parseInt(sBirthday.substring(0,1))*nShenshu14+Integer.parseInt(sBirthday.substring(1,2))*nShenshu13
                    +Integer.parseInt(sBirthday.substring(2,3))*nShenshu12+Integer.parseInt(sBirthday.substring(3,4))*nShenshu11
                    +Integer.parseInt(sBirthday.substring(4,5))*nShenshu10+Integer.parseInt(sBirthday.substring(5,6))*nShenshu9
                    +Integer.parseInt(sBirthday.substring(6,7))*nShenshu8+Integer.parseInt(sBirthday.substring(7,8))*nShenshu7
                    +Integer.parseInt(sPad_id.substring(0,1))*nShenshu6+Integer.parseInt(sPad_id.substring(1,2))*nShenshu5
                    +Integer.parseInt(sNewIdentitySeq.substring(0,1))*nShenshu4+Integer.parseInt(sNewIdentitySeq.substring(1,2))*nShenshu3
                    +Integer.parseInt(sNewIdentitySeq.substring(2,3))*nShenshu2)%nMod;
            sCheck = sCheckpairArray[nCheck];
            sIdentity_id = sCounty_code + sBirthday + sPad_id + sNewIdentitySeq + sCheck;
            if(sIdentity_id.length() == 15){

            }
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtil.showToast(ResourceUtil.getString(R.string.get_identity_id_error));
        }
        return sIdentity_id;

    }

}
